package com.example.mvcdemo.controller;

import com.example.mvcdemo.model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/2
 * Time: 15:28
 */
@RestController
@RequestMapping("/param")
public class ParamController {
    @RequestMapping("/p1")
    public String p1(String name) {
        return "接收到参数，name: " + name;
    }

    @RequestMapping("/p2")
    public String p2(Integer age) {
        return "接收到参数，age: " + age;
    }

    @RequestMapping("/p3")
    public String p3(int age) {
        return "接收到参数，age: " + age;
    }

    @RequestMapping("/p4")
    public String p4(String name, int age) {
        return "接收到参数，age: " + age + ", name: " + name;
    }

    @RequestMapping("/p5")
    public String p5(String name, int age, String gender) {
        return "接收到参数，age: " + age + ", name: " + name + ", gender: " + gender;
    }

    @RequestMapping("/p6")
    public String p6(User user) {
        System.out.println(user.getAge());
        System.out.println(user.getName());
        System.out.println(user.getGender());
        return "user: " + user;
    }

    // 给参数重命名
    @RequestMapping("/p7")
    public String p7(@RequestParam(value = "userName", required = false) String name) {
        return "接收到参数, name: " + name;
    }

    @RequestMapping("/p8")
    public String p8(String[] arr) {
        return "接收到参数, arr: " + List.of(arr);
    }

    @RequestMapping("/p9")
    public String p9(@RequestParam("arr") List<String> list) {
        return "接收到参数, List: " + list;
    }

    @RequestMapping("/p10")
    public String p10(@RequestBody User user) {
        return "接收到参数, user: " + user;
    }

    // 获取 url 中的参数
    @RequestMapping("/article/{articleId}")
    public String p11(@PathVariable("articleId") String articleId) {
        return "接收到参数, articleId: " + articleId;
    }

    @RequestMapping("/article/{articleId}/{name}")
    public String p12(@PathVariable("articleId") String id, @PathVariable("name")String name) {
        return "接收到参数, articleId: " + id + ", name: " + name;
    }

    //  上传文件
    @RequestMapping("/p13")
    public String p13(@RequestPart("fileName") MultipartFile file) {
        System.out.println(file.getOriginalFilename()); // 获取文件名称
        return "文件名称: " + file.getOriginalFilename();
    }


}
